package com.kuhyygy.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kuhyygy.R;

public class DialogActivity extends Activity {
	
	private static final String tag = "DialogActivity";
	
	private Resources res;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		res = getResources();
		setContentView(R.layout.dialogs);
		Button simpleDiaBtn = (Button) findViewById(R.id.simpleDia);
		simpleDiaBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showDialog();
			}
		});
		
		Button listDiaBtn = (Button) findViewById(R.id.listDia);
		listDiaBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showListDialog();
			}
		});
		
		Button selDiaBtn = (Button) findViewById(R.id.selectDia);
		selDiaBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showSelectDialog();
			}
		});
		
		Button cusDiaBtn = (Button) findViewById(R.id.customDia);
		cusDiaBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showCustomDialog();
			}
		});
	}


	protected void showCustomDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_signin, null);
		builder.setView(ll)	// 一定要在setView之前,把view准备好,不然下面的getText不会有数据
				.setPositiveButton(R.string.OK,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								EditText et = (EditText) ll.findViewById(R.id.username);
								alert(et.getText().toString());
							}
						})
				.setNegativeButton(R.string.CANCEL,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});
		builder.create().show();
	}


	protected void showSelectDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final List<Integer> mSelectedItems = new ArrayList<Integer>(); 
		builder.setTitle("你去过的城市")
			.setMultiChoiceItems(R.array.cities, null, new DialogInterface.OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int which, boolean isChecked) {
					if (isChecked) {
	                       mSelectedItems.add(which);
	                   } else if (mSelectedItems.contains(which)) {
	                       mSelectedItems.remove(Integer.valueOf(which));
	                   }
					
				}
			})
			.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   String str = "";
	            	   for (Integer i : mSelectedItems) {
	            		   str += res.getStringArray(R.array.cities)[i] + ",";
	            	   }
	            	   alert(str);
	               }
	           })
	           .setNegativeButton(R.string.CANCEL, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   dialog.dismiss();
	               }
	           });
		builder.create().show();
				
	}


	protected void showListDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("城市列表");
		builder.setItems(R.array.cities, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String selected = res.getStringArray(R.array.cities)[which];
				alert(selected);
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}


	protected void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("你确定要退出此页?");
		builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		               // User clicked OK button
		           }
		       });
		builder.setNegativeButton(R.string.CANCEL, new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		               // User cancelled the dialog
		           }
		       });
		AlertDialog dialog = builder.create();
		dialog.show();
	}


	/*
	public static class FireMissilesDialogFragment extends DialogFragment {
		
	    @Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
	        // Use the Builder class for convenient dialog construction
	        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	        builder.setMessage("你确定要退出此页?")
	               .setPositiveButton("是", new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   
	                   }
	               })
	               .setNegativeButton("否", new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                      getActivity().finish();
	                   }
	               });
	        // Create the AlertDialog object and return it
	        return builder.create();
	    }
	}
	*/
	
	private void alert(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}

}
